package com.example.textswitchtestdemo01.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.example.textswitchtestdemo01.util.NewsUtil2;

import java.util.List;

public class FirstActivity extends AppCompatActivity {
    private GridView gv_my;
    private GridView gv_wait;

    private NewsUtil2 newsUtil;
    private TextView tv_wait_none;

    public FirstActivity() {
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        InitToolbar.initToolbar(this, "第一页", true);

        newsUtil = new NewsUtil2(getApplicationContext());

        initView();
        initGrid();
    }

    private void initGrid() {
        gv_my.setAdapter(new BaseAdapter() {
            class ViewHolder {
                public View rootView;
                public Button btn_my;
                public Button btn_delete;

                public ViewHolder(View rootView) {
                    this.rootView = rootView;
                    this.btn_my = (Button) rootView.findViewById(R.id.btn_my);
                    this.btn_delete = (Button) rootView.findViewById(R.id.btn_delete);
                }

            }

            List<String> currentItems = newsUtil.getCurrentItems();

            @Override
            public int getCount() {
                return currentItems.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                if (convertView == null) {
                    view = View.inflate(getApplicationContext(), R.layout.gv_item_first_my, null);
                } else {
                    view = convertView;
                }
                final ViewHolder viewHolder = new ViewHolder(view);

                if (newsUtil.isLong()) {
                    viewHolder.btn_delete.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.btn_delete.setVisibility(View.GONE);
                }

                viewHolder.btn_my.setText(currentItems.get(position));

                viewHolder.btn_my.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (newsUtil.isLong()) {
                            newsUtil.setLong(false);
                        } else {
                            newsUtil.setLong(true);
                        }
                        initGrid();
                        return true;
                    }
                });
                viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (newsUtil.getCurrentItems().size() > 1) {
                            newsUtil.removeCurrentItems(viewHolder.btn_my.getText().toString().trim());
                            Log.d("FirstActivity", "remove:" + viewHolder.btn_my.getText().toString().trim());
                        } else {
                            Toast.makeText(getApplicationContext(), "不能再减少了", Toast.LENGTH_SHORT).show();
                        }

                        if (newsUtil.getWaitItems().size() == 0) {
                            tv_wait_none.setVisibility(View.VISIBLE);
                            gv_wait.setVisibility(View.GONE);
                        } else {
                            tv_wait_none.setVisibility(View.GONE);
                            gv_wait.setVisibility(View.VISIBLE);
                        }

                        initGrid();
                    }
                });
                return view;
            }
        });

        gv_wait.setAdapter(new BaseAdapter() {

            class ViewHolder {
                public View rootView;
                public Button btn_wait;
                public Button btn_add;

                public ViewHolder(View rootView) {
                    this.rootView = rootView;
                    this.btn_wait = (Button) rootView.findViewById(R.id.btn_wait);
                    this.btn_add = (Button) rootView.findViewById(R.id.btn_add);
                }

            }

            List<String> waitItems = newsUtil.getWaitItems();

            @Override
            public int getCount() {
                return waitItems.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                if (convertView == null) {
                    view = View.inflate(getApplicationContext(), R.layout.gv_item_first_wait, null);
                } else {
                    view = convertView;
                }

                final ViewHolder viewHolder = new ViewHolder(view);

                if (newsUtil.isLong()) {
                    viewHolder.btn_add.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.btn_add.setVisibility(View.GONE);
                }

                viewHolder.btn_wait.setText(waitItems.get(position));
                viewHolder.btn_wait.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (newsUtil.isLong()) {
                            newsUtil.setLong(false);
                        } else {
                            newsUtil.setLong(true);
                        }
                        initGrid();
                        return true;
                    }
                });
                viewHolder.btn_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        newsUtil.saveCurrentItems(viewHolder.btn_wait.getText().toString().trim());
                        Log.d("FirstActivity", "add:" + viewHolder.btn_wait.getText().toString().trim());

                        if (newsUtil.getWaitItems().size() == 0) {
                            tv_wait_none.setVisibility(View.VISIBLE);
                            gv_wait.setVisibility(View.GONE);
                        } else {
                            tv_wait_none.setVisibility(View.GONE);
                            gv_wait.setVisibility(View.VISIBLE);
                        }

                        initGrid();
                    }
                });
                return view;
            }
        });
    }

    private void initView() {
        gv_my = (GridView) findViewById(R.id.gv_my);
        gv_wait = (GridView) findViewById(R.id.gv_wait);
        tv_wait_none = (TextView) findViewById(R.id.tv_wait_none);
        //显示标题栏的返回图标
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        getSupportActionBar().setTitle("FirstAC");
//        getSupportActionBar().setDisplayShowTitleEnabled(false);
//        getSupportActionBar().setDisplayShowCustomEnabled(true);

        //Custom View
//        TextView titleTV = new TextView(getApplicationContext());
//        titleTV.setGravity(Gravity.CENTER);
//        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
//                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
//        titleTV.setText("FirstTitle");
//        titleTV.setTextSize(15);
//        titleTV.setTextColor(Color.WHITE);
//        titleTV.setLayoutParams(layoutParams);
//        getSupportActionBar().setCustomView(titleTV);

    }

    //标题栏返回图标的点击事件，方法1
//    @Override
//    public boolean onSupportNavigateUp() {
//        finish();
//        return super.onSupportNavigateUp();
//    }

    //标题栏返回图标的点击事件，方法2
//    @Override
//    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//        if (item.getItemId() == android.R.id.home) {
//            finish();
//            return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }
}
